﻿/**
 * File: worst_best_time_complexity.cs
 * Created Time: 2022-12-23
 * Author: haptear (haptear@hotmail.com)
 */

using NUnit.Framework;

namespace hello_algo.chapter_computational_complexity
{
    public class worst_best_time_complexity
    {
        /* 生成一个数组，元素为 { 1, 2, ..., n }，顺序被打乱 */
        static int[] randomNumbers(int n)
        {
            int[] nums = new int[n];
            // 生成数组 nums = { 1, 2, 3, ..., n }
            for (int i = 0; i < n; i++)
            {
                nums[i] = i + 1;
            }

            // 随机打乱数组元素
            for (int i = 0; i < nums.Length; i++)
            {
                var index = new Random().Next(i, nums.Length);
                var tmp = nums[i];
                var ran = nums[index];
                nums[i] = ran;
                nums[index] = tmp;
            }
            return nums;
        }

        /* 查找数组 nums 中数字 1 所在索引 */
        static int findOne(int[] nums)
        {
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] == 1)
                    return i;
            }
            return -1;
        }


        /* Driver Code */
        [Test]
        public void Test()
        {
            for (int i = 0; i < 10; i++)
            {
                int n = 100;
                int[] nums = randomNumbers(n);
                int index = findOne(nums);
                Console.WriteLine("\n数组 [ 1, 2, ..., n ] 被打乱后 = " + string.Join(",", nums));
                Console.WriteLine("数字 1 的索引为 " + index);
            }
        }
    }
}
